home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / DJGPP / CBGRX103.ZIP / contrib / libgrx / src / genellip.c < prev    next >
Text File  |  1993-12-06  |  7KB  |  194 lines

  1. /**
  2.  ** GENELLIP.C
  3.  **
  4.  **  Copyright (C) 1992, Csaba Biegl
  5.  **    820 Stirrup Dr, Nashville, TN, 37221
  6.  **    csaba@vuse.vanderbilt.edu
  7.  **
  8.  **  This file is distributed under the terms listed in the document
  9.  **  "copying.cb", available from the author at the address above.
  10.  **  A copy of "copying.cb" should accompany this file; if not, a copy
  11.  **  should be available from where this file was obtained.  This file
  12.  **  may not be distributed without a verbatim copy of "copying.cb".
  13.  **  You should also have received a copy of the GNU General Public
  14.  **  License along with this program (it is in the file "copying");
  15.  **  if not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  16.  **  Cambridge, MA 02139, USA.
  17.  **
  18.  **  This program is distributed in the hope that it will be useful,
  19.  **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  **  GNU General Public License for more details.
  22.  **/
  23.  
  24. #include "grx.h"
  25. #include "libgrx.h"
  26. #include "clipping.h"
  27. #include <string.h>
  28.  
  29. #define TABLEN  256
  30.  
  31. static unsigned int sintab[] = {
  32.     0,   101,   201,   302,      402,     503,    603,   704,
  33.       804,   904,  1005,  1105,  1205,  1306,  1406,  1506,
  34.      1606,  1706,  1806,  1906,  2006,  2105,  2205,  2305,
  35.      2404,  2503,  2603,  2702,  2801,  2900,  2999,  3098,
  36.      3196,  3295,  3393,  3492,  3590,  3688,  3786,  3883,
  37.      3981,  4078,  4176,  4273,  4370,  4467,  4563,  4660,
  38.      4756,  4852,  4948,  5044,  5139,  5235,  5330,  5425,
  39.      5520,  5614,  5708,  5803,  5897,  5990,  6084,  6177,
  40.      6270,  6363,  6455,  6547,  6639,  6731,  6823,  6914,
  41.      7005,  7096,  7186,  7276,  7366,  7456,  7545,  7635,
  42.      7723,  7812,  7900,  7988,  8076,  8163,  8250,  8337,
  43.      8423,  8509,  8595,  8680,  8765,  8850,  8935,  9019,
  44.      9102,  9186,  9269,  9352,  9434,  9516,  9598,  9679,
  45.      9760,  9841,  9921, 10001, 10080, 10159, 10238, 10316,
  46.     10394, 10471, 10549, 10625, 10702, 10778, 10853, 10928,
  47.     11003, 11077, 11151, 11224, 11297, 11370, 11442, 11514,
  48.     11585, 11656, 11727, 11797, 11866, 11935, 12004, 12072,
  49.     12140, 12207, 12274, 12340, 12406, 12472, 12537, 12601,
  50.     12665, 12729, 12792, 12854, 12916, 12978, 13039, 13100,
  51.     13160, 13219, 13279, 13337, 13395, 13453, 13510, 13567,
  52.     13623, 13678, 13733, 13788, 13842, 13896, 13949, 14001,
  53.     14053, 14104, 14155, 14206, 14256, 14305, 14354, 14402,
  54.     14449, 14497, 14543, 14589, 14635, 14680, 14724, 14768,
  55.     14811, 14854, 14896, 14937, 14978, 15019, 15059, 15098,
  56.     15137, 15175, 15213, 15250, 15286, 15322, 15357, 15392,
  57.     15426, 15460, 15493, 15525, 15557, 15588, 15619, 15649,
  58.     15679, 15707, 15736, 15763, 15791, 15817, 15843, 15868,
  59.     15893, 15917, 15941, 15964, 15986, 16008, 16029, 16049,
  60.     16069, 16088, 16107, 16125, 16143, 16160, 16176, 16192,
  61.     16207, 16221, 16235, 16248, 16261, 16273, 16284, 16295,
  62.     16305, 16315, 16324, 16332, 16340, 16347, 16353, 16359,
  63.     16364, 16369, 16373, 16376, 16379, 16381, 16383, 16384,
  64.     16384
  65. };
  66.  
  67. int _GrGenerateEllipse(int pt[][2],int cx,int cy,int rx,int ry)
  68. {
  69. #undef  WHEN_OUTSIDE
  70. #define WHEN_OUTSIDE    return(0)
  71. #define ulong    unsigned long
  72.     int x1,x2,y1,y2;
  73.     int nn,n2,n4;
  74.     int ii,dx,dy;
  75.     int gap,theta;
  76.  
  77.     if(rx < 0) rx = (-rx);
  78.     if(ry < 0) ry = (-ry);
  79.     x1 = cx - rx;  y1 = cy - ry;
  80.     x2 = cx + rx;  y2 = cy + ry;
  81.     CLIPSORTEDBOX(CURC,x1,y1,x2,y2);
  82.     if((rx == 0) || (ry == 0)) {
  83.         pt[0][0] = cx - rx; pt[0][1] = cy - ry;
  84.         pt[0][1] = cx + rx; pt[0][1] = cy + ry;
  85.         return(((rx == 0) && (ry == 0)) ? 1 : 2);
  86.     }
  87.     n2 = rx + ry;
  88.     if(n2 > 128) n2 >>= 1;
  89.     for(nn = 32; nn < n2; nn <<= 1);
  90.     if(n2 > MAX_ELLIPSE_PTS) nn = MAX_ELLIPSE_PTS;
  91.     n2 = nn >> 1;
  92.     n4 = n2 >> 1;
  93.     gap = TABLEN / n4;
  94.     for(ii = theta = 0; ii <= n4; ii++,theta += gap) {
  95.         dx = (int)((((ulong)rx * (ulong)sintab[TABLEN - theta]) + 8192UL) >> 14);
  96.         dy = (int)((((ulong)ry * (ulong)sintab[theta]) + 8192UL) >> 14);
  97.         pt[ii][0]     = cx + dx;
  98.         pt[ii][1]     = cy - dy;
  99.         pt[n2-ii][0] = cx - dx;
  100.         pt[n2-ii][1] = cy - dy;
  101.         pt[n2+ii][0] = cx - dx;
  102.         pt[n2+ii][1] = cy + dy;
  103.         pt[nn-ii][0] = cx + dx;
  104.         pt[nn-ii][1] = cy + dy;
  105.     }
  106.     return(nn);
  107. }
  108.  
  109.  
  110. /* for BCC2GRX -- to be made static later */
  111. int _grx_arc_xs, _grx_arc_ys;
  112. int _grx_arc_xe, _grx_arc_ye;
  113. int _grx_arc_xc, _grx_arc_yc;
  114.  
  115. int _GrGenerateEllipseArc(int pt[][2],int cx,int cy,int rx,int ry,int start,int end,int filled)
  116. {
  117. #define interp(p1,f1,p2,f2,div) \
  118.     (int)((((long)(p1) * (long)(f1)) + ((long)(p2) * (long)(f2))) / (long)(div))
  119.  
  120.     int tmp[MAX_ELLIPSE_PTS+1][2];
  121.     int nfinal,nn = _GrGenerateEllipse(tmp,cx,cy,rx,ry);
  122.     int angle1,angle2;
  123.     int index1,index2;
  124.     int fract1,fract2;
  125.     int x1,y1,x2,y2;
  126.  
  127.     if(nn <= 2) return(0);
  128.     if((angle1 = start % 3600) < 0) angle1 += 3600;
  129.     if((angle2 = end   % 3600) < 0) angle2 += 3600;
  130.     index1 = (int)(((ulong)angle1 * (ulong)nn) / 3600UL);
  131.     fract1 = (int)(((ulong)angle1 * (ulong)nn) % 3600UL);
  132.     index2 = (int)(((ulong)angle2 * (ulong)nn) / 3600UL);
  133.     fract2 = (int)(((ulong)angle2 * (ulong)nn) % 3600UL);
  134.     x1 = tmp[index1][0];
  135.     y1 = tmp[index1][1];
  136.     x2 = tmp[index2][0];
  137.     y2 = tmp[index2][1];
  138.     if(fract1 != 0) {
  139.         x1 = interp(x1,(3600 - fract1),tmp[index1+1][0],fract1,3600);
  140.         y1 = interp(y1,(3600 - fract1),tmp[index1+1][1],fract1,3600);
  141.     }
  142.     if(fract2 != 0) {
  143.         x2 = interp(x2,(3600 - fract2),tmp[index2+1][0],fract2,3600);
  144.         y2 = interp(y2,(3600 - fract2),tmp[index2+1][1],fract2,3600);
  145.         index2++;
  146.     }
  147.     if((x1 == x2) && (y1 == y2)) {
  148.         if(start == end) {
  149.         pt[0][0] = x1;
  150.         pt[0][1] = y1;
  151.         if(filled) {
  152.             pt[1][0] = cx;
  153.             pt[1][1] = cy;
  154.             return(2);
  155.         }
  156.         return(1);
  157.         }
  158.         memcpy(pt,tmp,(sizeof(int[2]) * nn));
  159.         return(-nn);
  160.     }
  161.     if(angle2 >= angle1) {
  162.         nfinal = index2 - index1 + 1;
  163.         memcpy(pt,&tmp[index1],(sizeof(int[2]) * nfinal));
  164.     }
  165.     else {
  166.         nfinal = index2 + nn - index1 + 1;
  167.         start = nn - index1;
  168.         end      = index2 + 1;
  169.         memcpy(pt,&tmp[index1],(sizeof(int[2]) * start));
  170.         memcpy(&pt[start],tmp,(sizeof(int[2]) * end));
  171.     }
  172.     pt[0][0] = x1;
  173.     pt[0][1] = y1;
  174.     pt[nfinal-1][0] = x2;
  175.     pt[nfinal-1][1] = y2;
  176.     _grx_arc_xs = x1; _grx_arc_ys = y1;
  177.     _grx_arc_xe = x2; _grx_arc_ye = y2;
  178.     _grx_arc_xc = cx; _grx_arc_yc = cy;
  179.     if(filled) {
  180.         pt[nfinal][0] = cx;
  181.         pt[nfinal][1] = cy;
  182.         return(nfinal + 1);
  183.     }
  184.     return(nfinal);
  185. }
  186.  
  187. void GrGetLastArcCoords(int *xs,int *ys,int *xe,int *ye,int *xc,int *yc)
  188. {
  189.     *xs = _grx_arc_xs; *ys = _grx_arc_ys;
  190.     *xe = _grx_arc_xe; *ye = _grx_arc_ye;
  191.     *xc = _grx_arc_xc; *yc = _grx_arc_yc;
  192. }
  193.  
  194.